package com.example.controller;

import com.example.result.ReturnResult;
import com.example.service.IPartService;
import com.example.vo.PartCreateFormVO;
import com.example.vo.PartQueryParamVO;
import com.example.vo.PartUpdateAndCheckinFormVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/part")
@CrossOrigin
public class PartController {
    @Resource
    private IPartService partService;

    @PostMapping("/create")
    public ReturnResult createPart(@RequestBody PartCreateFormVO partCreateFormVO){
        return partService.createPart(partCreateFormVO);
    }

    @GetMapping("/enum/query/{enumType}")
    public ReturnResult getEnum(@PathVariable("enumType") String enumType) {
        return partService.getEnum(enumType);
    }

    //查询指定分类结点的全部子结点：/api/ClassificationNode/getChildren?parentId=xxx&enable=xxx,
    //封装iDME公共的服务XDM_ClassificationNode_getChildren
    @GetMapping("/ClassificationNode/getChildren")
    public ReturnResult getClassificaionChildrenNodeByParams(@RequestParam(value = "parentId", required = true) String parentId,
                                                             @RequestParam(value = "enable", required = false, defaultValue = "true") String enable) {
        boolean enable_ = Boolean.parseBoolean(enable);//如果传参无法成功转化，则会抛出IllegalArgumentException
        return partService.getClassificationChildrenNodeByParams(parentId, enable_);
    }

    @GetMapping("/ClassificationNode")
    public ReturnResult getOneLevelClassificationCatalogue(
            @RequestParam(value = "enable", defaultValue = "true", required = false) String enable) {
        boolean enable_ = Boolean.parseBoolean(enable);//如果传参无法成功转化，则会抛出IllegalArgumentException
        return partService.getOneLevelClassificationCatalogue(enable_);
    }

    //查询指定分类结点的全部分类属性：/api/ClassificationNode/getCategoryNodeInfo?linkId=xxx,
    //封装iDME公共的服务 XDM_ClassificationNode_getCategoryNodeInfo
    @GetMapping("/ClassificationNode/getCategoryNodeInfo")
    public ReturnResult getCategoryNodeInfoByQueryParam(@RequestParam("id") String id) {
        ReturnResult categoryNodeInfo = partService.getCategoryNodeInfo(id);
        return categoryNodeInfo;
    }

    @PostMapping("/query")
    public ReturnResult query(@RequestBody PartQueryParamVO partQueryParamVO) {
        return partService.query(partQueryParamVO);
    }

    @PutMapping("/checkout")
    public ReturnResult checkoutPart(@RequestParam(value = "id") String masterId){
        Long num = Long.valueOf(masterId);
        return partService.checkoutPart(num);
    }

    @PutMapping("/undoCheckout")
    public ReturnResult undoCheckoutPart(@RequestParam(value = "id") String masterId){
        Long num = Long.valueOf(masterId);
        return  partService.undoCheckout(num);
    }

    @PutMapping("/updateAndCheckin")
    public ReturnResult updateAndCheckinPart(@RequestBody PartUpdateAndCheckinFormVO vo){
        return partService.updateAndCheckinPart(vo);
    }

    @DeleteMapping("/delete/{partId}")
    public ReturnResult delete(@PathVariable String partId) {
        return partService.delete(partId);
    }
}
